package org.example.stuview;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.IOException;

public class StudentView extends JFrame implements Runnable{
    HandleStudent handleStudent;
    String stuID;
    String stuName;
    JTable jTable;
    JButton test,lookScore;
    Object[] columnTitle = {"ID","姓名","成绩"};
    Object[][] tableData = new Object[][]{

    };
    DefaultTableModel tableDate = new DefaultTableModel(tableData,columnTitle);
    public StudentView(){
    }
    public void init(){
        this.setSize(1100,650);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setVisible(true);
        this.setTitle("学生页面");
        //设置ID:
        Font font = new Font(null,1,32);
        JLabel IDText = new JLabel("ID：     " + stuID);
        IDText.setFont(font);
        IDText.setBounds(300,35,500,35);
        this.getContentPane().add(IDText);
        //设置姓名:
        JLabel nameText = new JLabel("姓名：       " + stuName);
        nameText.setFont(font);
        nameText.setBounds(300,185,500,35);
        this.getContentPane().add(nameText);
        //设置考试按钮
        test = new JButton("开始考试");
        test.setBounds(400,275,160,30);
        this.getContentPane().add(test);
        //设置查询成绩按钮
        lookScore = new JButton("查询成绩");
        lookScore.setBounds(600,275,160,30);
        this.getContentPane().add(lookScore);
        //设置成绩表格
        showScore();

    }
    public void showScore(){
        jTable = new JTable(tableDate);
        JScrollPane jScrollPane = new JScrollPane(jTable);
        jScrollPane.setBounds(50,325,1000,250);
        this.getContentPane().add(jScrollPane);
    }

    public void studentListener() {
        handleStudent = new HandleStudent();
        handleStudent.setStudentView(this);
        test.addActionListener(handleStudent);
        lookScore.addActionListener(handleStudent);
    }
    public void setStuID(String stuID) {
        this.stuID = stuID;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Override
    public void run() {
        String s=null;
        while(true) {
            try{
                s=handleStudent.in.readUTF();
                System.out.println(s);
                if (s.length() == 0){
                    break;
                }
                if (s.startsWith("试卷第")){
                    TestView.test_paper = Integer.parseInt(s.substring(3));
                    System.out.println(TestView.test_paper);
                }if (s.startsWith("成绩")){
                    String score = s.substring(2);
                    System.out.println(score);
                    tableDate.addRow(new Object[]{stuID,stuName,score});
                }if (s.startsWith("第")){
                    System.out.println(s);
                    TestView.topics.add(s);
                    if (s.startsWith("第1")) {
                        TestView.TopicArea.setText(s + "\n");
                    }
                }

            }
            catch(IOException e) {
                break;
            }
        }
    }
}
